//
//  ProductListController.m
//  UI Demo
//
//  Created by Rainy Zou on 15/11/30.
//  Copyright © 2015年 Rainy Zou. All rights reserved.
//

#import "ProductListController.h"
#import "Product.h"
#import "ProductFrame.h"
#import "ProductCell.h"

@interface ProductListController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)NSArray *array;

@end

@implementation ProductListController

-(NSArray *)array
{
    if(_array ==nil){
        NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"productList" ofType:@"plist"]];
        NSMutableArray *mArray = [NSMutableArray array];
        for (NSDictionary *dict in arr) {
            Product *p = [[Product alloc]initWithDict:dict];
            ProductFrame *frame = [ProductFrame frameByProduct:p];
            frame.product = p;
            [mArray addObject:frame];
        }
        _array = [mArray copy];
    }
    return _array;
}

static NSString * const reuseIdentifier = @"CELLP";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *btn = [[UIButton alloc]init];
    UIImage *imageNormal = [UIImage imageNamed:@"head_back_normal"];
    imageNormal = [imageNormal stretchableImageWithLeftCapWidth:20 topCapHeight:20];
    UIImage *imageSelected = [UIImage imageNamed:@"head_back_selected"];
    imageSelected = [imageSelected stretchableImageWithLeftCapWidth:20 topCapHeight:20];
    btn.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 15);
    [btn setBackgroundImage:imageNormal forState:UIControlStateNormal];
    [btn setBackgroundImage:imageSelected forState:UIControlStateHighlighted];
    
    NSString *back = @"返回";
    CGSize size = [back sizeWithAttributes:@{NSFontAttributeName:btn.titleLabel.font}];
    
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    btn.titleLabel.textAlignment = NSTextAlignmentRight;
    
    [btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    btn.bounds = CGRectMake(0, 0, size.width + 30 , 40);
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem =leftItem;
    
    self.title = @"商品列表";
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    collectionView.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    
    [collectionView registerClass:[ProductCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    [self.view addSubview:collectionView];
}

-(void)clickBack
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSLog(@"Record Number:%ld", self.array.count);
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.pframe = self.array[indexPath.row];
    
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
#warning should go the shop detail page
    
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ProductFrame *frame = self.array[indexPath.row];
    return frame.productSize;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10 , 10, 10, 10);
}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

@end
